package com.avo.ramcache.anno.core.type;

import com.avo.ramcache.model.LruSoftHashMap;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;

import java.util.concurrent.ConcurrentHashMap;

public enum CacheType {

    LRU() {
        @Override
        public <PK, T> LruSoftHashMap<PK, T> createCache(int initialCapacity, int cachedSize, int concurrencyLevel) {
            ConcurrentLinkedHashMap.Builder<PK, T> builder = (new ConcurrentLinkedHashMap.Builder<PK, T>()).initialCapacity(initialCapacity).maximumWeightedCapacity(cachedSize).concurrencyLevel(concurrencyLevel);
            return new LruSoftHashMap<>(builder.build());
        }
    },

    MANUAL() {
        @Override
        public <PK, T> LruSoftHashMap<PK, T> createCache(int initialCapacity, int cachedSize, int concurrencyLevel) {
            return new LruSoftHashMap<>(new ConcurrentHashMap<>(initialCapacity, 0.75F, concurrencyLevel));
        }
    };

    public <PK, T> LruSoftHashMap<PK, T> createCache(int initialCapacity, int cachedSize, int concurrencyLevel) {
        throw new RuntimeException("重写该方法实现缓存");
    }
}
